﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using WebApplication3.Data;
using WebApplication3.Data.Entities;
using WebApplication3.Interfaces;
using WebApplication3.Models;
using WebApplication3.Services;

namespace WebApplication3.Controllers
{
	[Route("api/order")]
	[ApiController]
	public class OrderController : ControllerBase
	{
		private readonly Data.ApplicationDbContext applicationDbContext;
		private readonly ICapPublisher capPublisher;
		private readonly SFOrderService _sFOrderService;
		private readonly IAsyncRepository<OrderEntity> _orderEntityRepository;

		public OrderController(ApplicationDbContext applicationDbContext, 
			ICapPublisher capPublisher,
			SFOrderService sFOrderService,
			IAsyncRepository<OrderEntity> orderEntityRepository
			)
		{
			this.applicationDbContext = applicationDbContext;
			this.capPublisher = capPublisher;
			this._sFOrderService = sFOrderService;
			this._orderEntityRepository = orderEntityRepository;
		}

		// GET api/values  
		[HttpGet]
		public ActionResult<IEnumerable<string>> Get(string openid)
		{
			var orders = this.applicationDbContext.OrderEntities.Where(s => s.OpenId == openid)
				.Select(s=>new {
					id =s.Id,
					CreatedAt = s.CreatedAt.ToString("yyyy年MM月dd日 hh:mm:ss"),
					s.ReceiveAddress,
					s.ReceiveMobile,
					s.ReceiveName,  s.Status,s.StatusData
				})
				.ToList();
			return Ok(orders); 
		}

		[Route("getsf")]
		[HttpGet]
		public async Task<object> GetAsync(string openid,int id)
		{
			var order =await _orderEntityRepository.GetByIdAsync(id);
			//获取顺丰系统的订单信息
			var sfOrder = await _sFOrderService.QueryAsync(id);

			var status = "";
			switch (order.Status)
			{
				case 0:
					status = "下单";
					break;
				 
				case 2:
					status = "接单成功";
					break;
				case 3:
					status = "配送中";
					break;
				case 4:
					status = "取消订单";
					break;
				 
				case 6:
					status = "已完成";
					break;
				default:
					break;
			}
			return new { status = status, order = order, sfOrder = sfOrder };
			 
			 
		}

		[Route("count")]
		[HttpGet]
		public ActionResult<int> GetCount(string openid)
		{
			var orders = this.applicationDbContext.OrderEntities.Where(s => s.OpenId == openid).Count();
			return Ok(orders);

		}

		// GET api/values/5
		[HttpGet("{id}")]
		public ActionResult<string> Get(int id)
		{
			return "value";
		}

		#region 下单
		// POST api/values
		[Route("create")]
		[HttpPost]
		public async Task<ActionResult<string>> PostAsync([FromBody] OrderCreateModel model)
		{
			if (!ModelState.IsValid)
			{
				return ValidationProblem();
			}
			var order = new Data.Entities.OrderEntity
			{
				OpenId = model.OpenId,
				ProductName = model.ProductName,
				ReceiveAddress = model.ReceiveAddress,
				ReceiveMobile = model.ReceiveMobile,
				ReceiveName = model.ReceiveName,
				WeightGram = model.WeightGram,
				CustomerLatitude = model.CustomerLatitude,
				CustomerLongitude = model.CustomerLongitude,
				ReceiveLatitude = model.ReceiveLatitude,
				ReceiveLongitude = model.ReceiveLongitude, CreatedAt=DateTime.Now
			};
			var addOrder = await this.applicationDbContext.OrderEntities.AddAsync(order);
			await this.applicationDbContext.SaveChangesAsync();


			//using (var trans = this.applicationDbContext.Database.BeginTransaction(this.capPublisher, autoCommit: true))
			//{



			//	//业务代码 
			//	await this.capPublisher.PublishAsync (ConstData.CreateOrderPublisherKey, order,"aaa");
			//}
			return Ok(addOrder.Entity.Id);


		}
		public void aaa()
		{

		}

		 

		#endregion



		// PUT api/values/5
		[HttpPut("{id}")]
		public void Put(int id, [FromBody] string value)
		{
		}

		// DELETE api/values/5
		[HttpDelete("{id}")]
		public void Delete(int id)
		{
		}
	}
}
